package com.hdoit.ares.selfcommon.utils;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.imageio.ImageIO;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;



public class QRCodeUtils {
	public static Map<EncodeHintType, Object> getDecodeHintType() {
		// 用于设置QR二维码参数
		Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
		// 设置QR二维码的纠错级别（H为最高级别）具体级别信息
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
		// 设置编码方式
		hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
		
		hints.put(EncodeHintType.MARGIN, 1);

		return hints;
	}

	public static BufferedImage fileToBufferedImage(BitMatrix bm) {
		BufferedImage image = null;
		try {
			int w = bm.getWidth(), h = bm.getHeight();
			image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

			for (int x = 0; x < w; x++) {
				for (int y = 0; y < h; y++) {
					image.setRGB(x, y, bm.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return image;
	}

	public static BufferedImage getQR_CODEBufferedImage(String content) {
		return getQR_CODEBufferedImage(content,BarcodeFormat.QR_CODE);
	}
	
	public static BufferedImage getQR_CODEBufferedImage(String content,Map<EncodeHintType, ?> hints) {
		return getQR_CODEBufferedImage(content,BarcodeFormat.QR_CODE,300,300,hints);
	}
	
	public static BufferedImage getQR_CODEBufferedImage(String content,BarcodeFormat barcodeFormat) {
		return getQR_CODEBufferedImage(content,barcodeFormat,300,300);
	}
	
	public static BufferedImage getQR_CODEBufferedImage(String content,BarcodeFormat barcodeFormat, int width, int height) {
		return getQR_CODEBufferedImage(content,barcodeFormat,width,height,getDecodeHintType());
	}
	public static BufferedImage getQR_CODEBufferedImage(String content,BarcodeFormat barcodeFormat, int width, int height,Map<EncodeHintType, ?> hints) {
		MultiFormatWriter multiFormatWriter = null;
		BitMatrix bm = null;
		BufferedImage image = null;
		try {
			multiFormatWriter = new MultiFormatWriter();

			// 参数顺序分别为：编码内容，编码类型，生成图片宽度，生成图片高度，设置参数
			bm = multiFormatWriter.encode(content, barcodeFormat, width,
					height, hints);

			int w = bm.getWidth();
			int h = bm.getHeight();
			image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

			// 开始利用二维码数据创建Bitmap图片，分别设为黑（0xFFFFFFFF）白（0xFF000000）两色
			for (int x = 0; x < w; x++) {
				for (int y = 0; y < h; y++) {
					image.setRGB(x, y, bm.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
				}
			}
		} catch (WriterException e) {
			e.printStackTrace();
		}
		return image;
	}

	public static void decodeQR_CODE2ImageFile(BitMatrix bm, String imageFormat,File file) {
		try {
			if (null == file || file.getName().trim().isEmpty()) {
				throw new IllegalArgumentException("文件异常，或扩展名有问题！");
			}

			BufferedImage bi = fileToBufferedImage(bm);
			ImageIO.write(bi, "png", file);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void decodeQR_CODE2OutputStream(BitMatrix bm, String imageFormat,
			OutputStream os) {
		try {
			BufferedImage image = fileToBufferedImage(bm);
			ImageIO.write(image, imageFormat, os);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void addLogo_QRCode(BufferedImage image, File logoPic) {
		if (!logoPic.isFile()) {
			System.out.print("file not find !");
			System.exit(0);
		}
		BufferedImage logo;
		try {
			logo = ImageIO.read(logoPic);
			addLogo_QRCode(image,logo);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	public static void addLogo_QRCode(BufferedImage image, BufferedImage logo) {
		try {
			Graphics2D g = image.createGraphics();
			/**
			 * 读取Logo图片
			 */
			float logoRate=(float)(logo.getWidth())/(float)(logo.getHeight());
			float widthLogo=0;
			float heightLogo=0;
			
			if(logo.getWidth()<45&&logo.getHeight()<45){
				widthLogo = logo.getWidth();heightLogo = logo.getHeight();
			}else{
				if(logo.getWidth()>=45){
					widthLogo=45;heightLogo=45/logoRate;
				}else{
					heightLogo=45;widthLogo=45*logoRate;
				}
			}
			
			// 计算图片放置位置
			int x = (image.getWidth() - (int)widthLogo) / 2;
			int y = (image.getHeight() - (int)heightLogo) / 2;

			// 开始绘制图片
			g.drawImage(logo, x, y, (int)widthLogo, (int)heightLogo, null);
			g.drawRoundRect(x, y,(int)widthLogo, (int)heightLogo, 15, 15);
			g.setStroke(new BasicStroke(1));
			g.setColor(Color.WHITE);
			g.drawRect(x, y, (int)widthLogo, (int)heightLogo);

			g.dispose();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
